// pages/fillcompany/fillcompany.js
var app = getApp();
var api = require('../../api.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    index:0,
    array: [],
    array_index:0,
    id:0,
  },

  bindPickerChange: function (e) {
    var id = this.data.array[e.detail.value].id
    this.setData({
      array_index: e.detail.value,
      id: id
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getcompany()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  getcompany(){
    var that = this
    app.request({
      url: api.user.getcompany,
      method: 'POST',
      data: {},
      success: function (resp) {
        if (resp.errcode == 0) {
          that.setData({
            array: resp.data,
            id: resp.data[0].id,
          })
        }
      },
      fail: function (resp) {
        wx.showModal({
          title: '警告',
          content: res.errmsg,
          showCancel: false
        })
      },
      complete: function () {

      }
    })
  },

  formSubmit(e) {
    console.log('form发生了submit事件，携带数据为：', e.detail.value)
    
    var cid = this.data.id
    var bumen = e.detail.value.bumen
    var name = e.detail.value.name
    var password = e.detail.value.password

    if (bumen == '' || name == '' || password=='') { 
      wx.showToast({
        icon:'none',
        title: '信息不完整',
      })
      return 0
    }

    // wx.showLoading({
    //   title: '验证中',
    // })
    app.request({
      url: api.user.verifypassword,
      method: 'POST',
      data: {
        bumen: bumen,
        name: name,
        password: password,
        cid: cid
      },
      success: function (resp) {
        // wx.hideLoading();
        console.log(resp)
        if (resp.errcode == 0) {
          wx.showToast({
            icon:'none',
            title: resp.data,
          })
        }
        if (resp.errcode == 1) {
          wx.showToast({
            title: resp.data,
          })
          setTimeout(function(){
            wx.hideToast()
            var user_info = wx.getStorageSync('user_info')
            user_info.company_id = cid
            wx.setStorageSync('user_info', user_info)
            wx.redirectTo({
              url: '/pages/index/index',
            })
          }, 2000)
        }
      },
      fail: function (resp) {
        wx.showModal({
          title: '警告',
          content: res.errmsg,
          showCancel: false
        })
      },
      complete: function () {
        // wx.hideLoading();
      }
    })
  }

})